package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.StopCaseModel;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.StopCaseModelElastic;
import cn.fintecher.pangolin.service.dataimp.repository.CaseInfoRepository;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 17:23 2019/11/5
 */

@Service("cancelStopTask")
public class CancelStopTask {
    private static final Logger log = LoggerFactory.getLogger(CancelStopTask.class);

    @Autowired
    private StopCaseModelElastic stopCaseModelElastic;

    @Autowired
    private ProcessCaseFuture processCaseFuture;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    /**
     * 撤销停催
     */
    @Async
    @Transactional
    public void cancelStop(BoolQueryBuilder builder, DataImportRecord dataImportRecord, UserModel userModel) {
        //创建任务集合
        List<CompletableFuture<List<CaseInfo>>> list = new ArrayList<>();
        Iterable<StopCaseModel> iterable = stopCaseModelElastic.search(builder);
        List<StopCaseModel> stopCaseModels = IterableUtils.toList(iterable);
        //分页处理信息
        int pageSize = 2000;
        int pageNumber = (int) Math.ceil((double) stopCaseModels.size() / (double) pageSize);
        for (int i = 0; i < pageNumber; i++) {
            List<StopCaseModel> models = stopCaseModels.stream().skip(i * pageSize).limit(pageSize).collect(Collectors.toList());
            CompletableFuture<List<CaseInfo>> task = processCaseFuture.processCaseInfo(null, models, 1);
            list.add(task);
        }
        try {
            List<CaseInfo> caseInfos = new ArrayList<>();
            for (CompletableFuture<List<CaseInfo>> future : list) {
                caseInfos.addAll(future.get());
            }
            stopCaseModels.forEach(e -> e.setFlag(1));
            RepositoryUtil.saveData(stopCaseModelElastic, stopCaseModels);
            RepositoryUtil.saveData(caseInfoRepository, caseInfos);
            dataImportRecord.setStatus(ImportDataExcelStatus.BILL_REVOKE);
            dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(dataImportRecord);
        } catch (InterruptedException | ExecutionException e) {
            log.error(e.getMessage(), e);
            dataImportRecord.setStatus(ImportDataExcelStatus.UN_CONFIRMED);
            dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(dataImportRecord);
        }
    }
}
